from tortoise import fields
from tortoise.models import Model
from enum import IntEnum

class GoodsStatus(IntEnum):
    DISABLED = 0
    ENABLED = 1

class Goods(Model):
    """商品信息模型"""
    
    id = fields.IntField(pk=True)
    code = fields.CharField(
        max_length=50,
        unique=True,
        null=False,
        description='商品编码'
    )
    name = fields.CharField(max_length=255, description='商品名称')
    category = fields.CharField(max_length=50, null=True, description='商品类别')
    status = fields.IntField(default=1, description='商品状态：1-正常，0-下架')
    create_at = fields.DatetimeField(auto_now_add=True, description='创建时间')
    update_at = fields.DatetimeField(auto_now=True, description='更新时间')

    # 反向关系
    warehouse_relations: fields.ReverseRelation["WarehouseGoods"]

    class Meta:
        table = "goods"
        table_description = "商品信息表"

    def __str__(self):
        return f"{self.name} ({self.code})"

    @property
    def is_active(self) -> bool:
        """检查商品是否处于上架状态"""
        return self.status == GoodsStatus.ENABLED 